package cn.edu.xmu.oomall.customer.dao;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.mapper.ShoppingCartPoMapper;
import cn.edu.xmu.oomall.customer.model.bo.Carts;
import cn.edu.xmu.oomall.customer.model.po.ShoppingCartPo;
import cn.edu.xmu.oomall.customer.model.po.ShoppingCartPoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.setPoModifiedFields;
import static cn.edu.xmu.privilegegateway.annotation.util.Common.setPoCreatedFields;
import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Jiawei Zheng
 * @date 2021-12-04
 */
@Repository
public class ShoppingCartsDao {
    @Autowired
    private ShoppingCartPoMapper shoppingCartPoMapper;

    private static Logger logger = LoggerFactory.getLogger(Common.class);


    public ReturnObject getCarts(Long userId, Integer page, Integer pageSize){
        try{
            PageHelper.startPage(page, pageSize);
            ShoppingCartPoExample example = new ShoppingCartPoExample();
            ShoppingCartPoExample.Criteria criteria = example.createCriteria();
            criteria.andCustomerIdEqualTo(userId);
            List<ShoppingCartPo> poList = shoppingCartPoMapper.selectByExample(example);
            List<Carts> boList = new ArrayList<>();
            for(ShoppingCartPo po:poList){
                boList.add(cloneVo(po, Carts.class));
            }
            PageInfo pageInfo = PageInfo.of(poList);
            pageInfo.setList(boList);
            return new ReturnObject(pageInfo);

        }
        catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /**
     * 买家将商品加入购物车
     * @param userId
     * @param userName
     * @param carts
     * @return
     */
    public ReturnObject addCarts(Long userId, String userName, Carts carts){
        try {
            ShoppingCartPo po = cloneVo(carts, ShoppingCartPo.class);
            setPoCreatedFields(po, userId, userName);
            shoppingCartPoMapper.insert(po);
            return new ReturnObject(cloneVo(po, Carts.class));
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /**
     * 买家清空购物车
     * @param userId
     * @return
     */
    public ReturnObject clearCarts(Long userId){
        try{
            ShoppingCartPoExample example = new ShoppingCartPoExample();
            ShoppingCartPoExample.Criteria criteria = example.createCriteria();
            criteria.andCustomerIdEqualTo(userId);
            int ret = shoppingCartPoMapper.deleteByExample(example);
            //没有对应记录，删除失败
            if(ret==0){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            return new ReturnObject(ReturnNo.OK);

        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /**
     * 买家修改购物车商品的数量或规格
     * @param userId
     * @param userName
     * @param id
     * @param carts
     * @return
     */
    public ReturnObject updateCarts(Long userId, String userName, Long id, Carts carts){
        try {
            //根据id找出要修改的购物车记录
            ShoppingCartPo po = shoppingCartPoMapper.selectByPrimaryKey(id);
            //找不到
            if (po == null) {
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            if(!po.getCustomerId().equals(userId)){
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            if(!po.getProductId().equals(carts.getProductId())){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            //如果传入数量为0,则删除这条购物车记录
            if(carts.getQuantity()==0){
                shoppingCartPoMapper.deleteByPrimaryKey(id);
                return new ReturnObject(ReturnNo.OK);
            }
            setPoModifiedFields(po, userId, userName);
            po.setProductId(carts.getProductId());
            po.setQuantity(carts.getQuantity());
            shoppingCartPoMapper.updateByPrimaryKeySelective(po);
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /***
     * 买家删除购物车中商品
     * @param id 购物车记录id
     * @return ReturnObject
     */
    public ReturnObject deleteCarts(Long userId, Long id){
        try{
            ShoppingCartPo po = shoppingCartPoMapper.selectByPrimaryKey(id);
            if(!po.getCustomerId().equals(userId)){
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            int ret = shoppingCartPoMapper.deleteByPrimaryKey(id);
            if(ret==0){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            else{
                return new ReturnObject(ReturnNo.OK);
            }
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    public ReturnObject selectCartsByProductId(Long userId, Long productId){
        try {
            ShoppingCartPoExample example = new ShoppingCartPoExample();
            ShoppingCartPoExample.Criteria criteria = example.createCriteria();
            criteria.andCustomerIdEqualTo(userId);
            criteria.andProductIdEqualTo(productId);
            List<ShoppingCartPo> poList = shoppingCartPoMapper.selectByExample(example);
            if (poList != null) {
                return new ReturnObject(cloneVo(poList.get(0), Carts.class));
            }
            return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }

    }

    public ReturnObject updateCart(Carts bo){
        try {
            ShoppingCartPo po = cloneVo(bo, ShoppingCartPo.class);
            shoppingCartPoMapper.updateByPrimaryKeySelective(po);
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

}
